package cn.com.qtgl.sso.oidc.comp.service;

import cn.com.qtgl.oauth2.TokenManager;
import cn.com.qtgl.sso.oidc.comp.config.SsoConfig;
import cn.com.qtgl.sso.oidc.comp.config.SsoScopesConfig;
import cn.melonlib.cores.aop.comp.web.WebInfo;
import lombok.Getter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
@Getter
public class OAuth2Configuration {

    @Resource
    private SsoConfig ssoConfig;

    @Resource
    private SsoScopesConfig ssoScopesConfig;

    @Resource
    private WebInfo webInfo;

    @Bean
    public TokenManager tokenManager() {
        return new TokenManager(ssoConfig.getOauthAuthorityUrl().startsWith("http")?ssoConfig.getOauthAuthorityUrl():webInfo.getServiceOrigin()+ssoConfig.getOauthAuthorityUrl()
                , ssoConfig.getOauthClientId(), ssoConfig.getOauthClientSecret(), ssoScopesConfig.getClientCredentials());
    }
}
